#ifndef SKELETON_HPP_
#define SKELETON_HPP_

#include <aerDefs.hpp>


///
/// TODO :
/// don't use GLM quaternion
///


/// Armature ==========================

struct Joint_t
{
  char           name[32];//
  aer::Matrix4x4 invBindPoseMatrix;//
  aer::I32       parentId;
};

/// Animation ==========================

struct JointPose_t
{
  aer::Quaternion qRotation; 
  aer::Vector3    vTranslation;
  aer::F32        fScale;
};

// not really a 'keyframe' as we use one per frame,
// could rename it 'AnimSample'
struct Keyframe_t
{
  JointPose_t* localPoses;
};

struct Sequence_t
{
  char        name[32];//
  Keyframe_t* keyframes;
  aer::U32    frameCount;
  aer::F32    framesPerSecond;
  bool        bIsLooping;
};


//---------------------------

class Skeleton
{
  friend class DebugRenderer;//
  
  private:
    /// Armature
    Joint_t* m_joints;
    aer::U32 m_jointCount;
    
    /// Animation
    Sequence_t* m_sequences;
    aer::U32    m_sequenceCount;
  
    Keyframe_t      m_lerpKF;
    aer::Matrix4x4* m_animGlobalPos;
    
    
    /// Debug
    aer::Matrix4x4* m_dbgGlobalBind; // global bind matrix for bone display
    
  
  public:
    Skeleton()
        : m_joints(NULL),
          m_jointCount(0u),
          m_sequences(NULL),
          m_sequenceCount(0u),
          m_animGlobalPos(NULL),
          m_dbgGlobalBind(NULL)
    {
      m_lerpKF.localPoses = NULL;
    }
    
    ~Skeleton()
    {
      AER_SAFE_DELETEV( m_joints );
      AER_SAFE_DELETEV( m_sequences );
      AER_SAFE_DELETEV( m_lerpKF.localPoses );
      AER_SAFE_DELETEV( m_animGlobalPos );
      
      AER_SAFE_DELETEV( m_dbgGlobalBind );
    }
    
    /// Load a Skeleton (SKL) file containing bones structures and animations
    void load(const char *name);
    
    void computePose( const aer::uint sequence_id,        // animation sequence where to retrieve the pose
                      const float anim_startTime,         // animation start
                      aer::Matrix4x4 *skinningMatrices);  // skinning matrices output
    
};



#endif //SKELETON_HPP_
